package com.yvan.es.client;

import com.google.gson.Gson;
import com.yvan.es.EsStat;
import com.yvan.es.RestFulResult;
import com.yvan.platform.StringUtils;
import com.yvan.spring.autoconfig.EsAutoConfiguration;
import org.elasticsearch.action.search.SearchAction;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.IndicesOptions;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryParseContext;
import org.elasticsearch.search.SearchModule;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.io.IOException;
import java.util.Collections;

/**
 * Created by Administrator on 2018/9/12.
 */
public class EsTransportClient {

    public boolean isEnable() {

        if (EsAutoConfiguration.client != null) {
            return true;
        }
        return false;
    }

    public RestFulResult search(String path, String body) throws IOException {
        path = path.replace("/_search", "");
        path = path.replace("?ignore_unavailable=true", "");
        return search(new SearchRequest().indices(path), body);
    }

    public RestFulResult search(String index, String type, String body) throws IOException {
        if (StringUtils.isNullOrEmpty(type)) {
            return search(new SearchRequest().indices(index), body);
        }
        return search(new SearchRequest().indices(index).types(type), body);
    }

    public RestFulResult search(SearchRequest request, String body) throws IOException {

        long startTime = System.currentTimeMillis();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchModule searchModule = new SearchModule(Settings.EMPTY, false, Collections.emptyList());
        SearchResponse response = null;
        try (XContentParser parser = XContentFactory.xContent(XContentType.JSON)
                .createParser(new NamedXContentRegistry(searchModule.getNamedXContents()), body)) {
            searchSourceBuilder.parseXContent(new QueryParseContext(parser));
            SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(EsAutoConfiguration.client, SearchAction.INSTANCE);
            response = searchRequestBuilder
                    .setIndices(request.indices())
                    .setTypes(request.types())
                    .setSource(searchSourceBuilder)
                    .setIndicesOptions(IndicesOptions.fromOptions(true, false, true, false))
                    .execute().actionGet();
        } catch (IOException ex) {
            throw ex;
        }

        String responseBody = response.toString();
        RestFulResult result = new RestFulResult(new Gson());
        result.setJsonString(responseBody);
        EsStat.record(startTime, System.currentTimeMillis(), "", body.length() * 2L, responseBody.length() * 2L);
        return result;
    }
}
